.TH std::filesystem::relative,std::filesystem::proximate 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::filesystem::relative,std::filesystem::proximate \- std::filesystem::relative,std::filesystem::proximate

.SH Synopsis
   Defined in header <filesystem>
   path relative( const std::filesystem::path& p,                     \fB(1)\fP \fI(since C++17)\fP
                  std::error_code& ec );
   path relative( const std::filesystem::path& p,
                  const std::filesystem::path& base =                 \fB(2)\fP \fI(since C++17)\fP
   std::filesystem::current_path() );
   path relative( const std::filesystem::path& p,

                  const std::filesystem::path& base,                  \fB(3)\fP \fI(since C++17)\fP

                  std::error_code& ec );
   path proximate( const std::filesystem::path& p,                    \fB(4)\fP \fI(since C++17)\fP
                   std::error_code& ec );
   path proximate( const std::filesystem::path& p,
                   const std::filesystem::path& base =                \fB(5)\fP \fI(since C++17)\fP
   std::filesystem::current_path() );
   path proximate( const std::filesystem::path& p,

                   const std::filesystem::path& base,                 \fB(6)\fP \fI(since C++17)\fP

                   std::error_code& ec );

   1) Returns relative(p, current_path(), ec).
   2,3) Returns p made relative to base. Resolves symlinks and normalizes both p and
   base before other processing. Effectively returns
   std::filesystem::weakly_canonical(p).lexically_relative(std::filesystem::weakly_canonical(base))
   or std::filesystem::weakly_canonical(p,
   ec).lexically_relative(std::filesystem::weakly_canonical(base, ec)), except the
   error code form returns path() at the first error occurrence, if any.
   4) Returns proximate(p, current_path(), ec).
   5,6) Effectively returns
   std::filesystem::weakly_canonical(p).lexically_proximate(std::filesystem::weakly_canonical(base))
   or std::filesystem::weakly_canonical(p,
   ec).lexically_proximate(std::filesystem::weakly_canonical(base, ec)), except the
   error code form returns path() at the first error occurrence, if any.

.SH Parameters

   p    - an existing path
   base - base path, against which p will be made relative/proximate
   ec   - error code to store error status to

.SH Return value

   1) p made relative against current_path().
   2,3) p made relative against base.
   4) p made proximate against current_path().
   5,6) p made proximate against base.

.SH Exceptions

   Any overload not marked noexcept may throw std::bad_alloc if memory allocation
   fails.

   2,5) Throws std::filesystem::filesystem_error on underlying OS API errors,
   constructed with p as the first path argument, base as the second path argument, and
   the OS error code as the error code argument.
   1,3,4,6) Sets a std::error_code& parameter to the OS API error code if an OS API
   call fails, and executes ec.clear() if no errors occur.

.SH Example


// Run this code

 #include <filesystem>
 #include <iostream>

 void show(std::filesystem::path x, std::filesystem::path y)
 {
     std::cout << "x:\\t\\t " << x << "\\ny:\\t\\t " << y << '\\n'
               << "relative(x, y):  "
               << std::filesystem::relative(x, y) << '\\n'
               << "proximate(x, y): "
               << std::filesystem::proximate(x, y) << "\\n\\n";
 }

 int main()
 {
     show("/a/b/c", "/a/b");
     show("/a/c", "/a/b");
     show("c", "/a/b");
     show("/a/b", "c");
 }

.SH Possible output:

 x:               "/a/b/c"
 y:               "/a/b"
 relative(x, y):  "c"
 proximate(x, y): "c"

 x:               "/a/c"
 y:               "/a/b"
 relative(x, y):  "../c"
 proximate(x, y): "../c"

 x:               "c"
 y:               "/a/b"
 relative(x, y):  ""
 proximate(x, y): "c"

 x:               "/a/b"
 y:               "c"
 relative(x, y):  ""
 proximate(x, y): "/a/b"

.SH See also

   path                represents a path
   \fI(C++17)\fP             \fI(class)\fP
   absolute            composes an absolute path
   \fI(C++17)\fP             \fI(function)\fP
   canonical           composes a canonical path
   weakly_canonical    \fI(function)\fP
   \fI(C++17)\fP
   lexically_normal    converts path to normal form
   lexically_relative  converts path to relative form
   lexically_proximate converts path to proximate form
                       \fI(public member function of std::filesystem::path)\fP
